<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

/**
 * 订单管理
 * Class Slide
 * @package app\admin\controller
 */
class order extends AdminBase
{

    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 订单管理
     * @return mixed
     */
    public function index($keyword = '', $page = 1,$status = 'all')
    {
        $map = [];
        if ($keyword) {
            $map['order_number'] = ['like', "%{$keyword}%"];
        }
        if($status!='all'){
            $map['status'] = $status;
        }
        $order_list  = Db::name('order')->where($map)->order('id desc')->select();
        foreach ($order_list as $k=>$v){
            $order_list[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
            $order_list[$k]['name'] = Db::name('good')->where(['id'=>$v['good_id']])->value('name');
            $order_list[$k]['image'] = Db::name('good')->where(['id'=>$v['good_id']])->value('image');
            $order_list[$k]['username'] = Db::name('user')->where(['id'=>$v['user_id']])->value('username');
            $order_list[$k]['mobile'] = Db::name('user')->where(['id'=>$v['user_id']])->value('mobile');
            switch ($v['status']) {
                case 0:
                    $order_list[$k]['status'] = '待付款';
                    break;
                case 1:
                    $order_list[$k]['status'] = '已付款';
                    break;
                case 2:
                    $order_list[$k]['status'] = '已发货';
                    break;
                case 3:
                    $order_list[$k]['status'] = '已完成';
                    break;
            }
        }
        return $this->fetch('index', ['order_list' => $order_list,'img_url'=>'http://' . $_SERVER['SERVER_NAME'] .'/public','keyword' => $keyword,'status'=>$status]);
    }

    /**
     * 编辑订单
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $order = Db::name('order')->where('id',$id)->find();
        $order['add_time'] = date('Y-m-d H:i',$order['add_time']);
        $order['name'] = Db::name('good')->where(['id'=>$order['good_id']])->value('name');
        $order['image'] = Db::name('good')->where(['id'=>$order['good_id']])->value('image');
        $order['username'] = Db::name('user')->where(['id'=>$order['user_id']])->value('username');
        $order['mobile'] = Db::name('user')->where(['id'=>$order['user_id']])->value('mobile');
        $order['express_number'] = Db::name('express')->where(['order_id'=>$order['id']])->value('express_number');
        $order['express_id'] = Db::name('express')->where(['order_id'=>$order['id']])->value('express_id');
        switch ($order['status']) {
            case 0:
                $order['status_name'] = '待付款';
                break;
            case 1:
                $order['status_name'] = '已付款';
                break;
            case 2:
                $order['status_name'] = '已发货';
                break;
            case 3:
                $order['status_name'] = '已完成';
                break;
        }
        $express = Db::name('express_class')->select();
        return $this->fetch('edit', ['order' => $order,'img_url'=>'http://' . $_SERVER['SERVER_NAME'] .'/public','express'=>$express]);
    }
    /*
     * 发货
     * */
    public function send($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $express_save = [
                'order_id'=>$id,
                'express_number'=>$data['express_number'],
                'express_id'=>$data['express_id'],
                'express_time'=>time(),
                'add_time'=>time()
            ];
            Db::startTrans();
            try{
                Db::name('express')->insertGetId($express_save);
                Db::name('order')->update(['id'=>$id,'status'=>2]);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('更新成功');
        }
    }
}